package com.haizhi.crm.biz.service.impl;

import com.haizhi.crm.biz.dao.FavouriteDao;
import com.haizhi.crm.biz.model.FavouriteDo;
import com.haizhi.crm.biz.model.FavouriteQo;
import com.haizhi.crm.biz.service.FavouriteService;
import com.haizhi.crm.common.constant.BizStatus;
import com.haizhi.crm.common.constant.CrmSchema;
import com.haizhi.crm.common.exception.UnexpectedStatusException;
import com.haizhi.crm.common.model.Response;
import com.haizhi.crm.common.util.CommonUtils;
import com.haizhi.crm.persist.builder.PersistFactory;
import com.haizhi.crm.persist.builder.PersistReqBuilder;
import com.haizhi.crm.persist.builder.SourceBuilder;
import com.haizhi.crm.persist.constant.PersistTarget;
import com.haizhi.crm.persist.service.PersistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * 收藏service
 * Created by yangyijun on 2018/3/31.
 */
@Service
public class FavouriteServiceImpl implements FavouriteService {

    @Autowired
    private FavouriteDao favouriteDao;
    @Autowired
    private PersistService persistService;

    /**
     * 收藏／取消收藏
     *
     * @return
     */
    @Transactional(readOnly = false)
    public Response favourite(FavouriteQo favouriteQo) {
        try {
            Long userId = CommonUtils.getUserID();
            String entityKey = favouriteQo.getEntityKey();
            String entityType = favouriteQo.getEntityType();
            boolean isFavourite = favouriteQo.isFavourite();

            FavouriteDo favourite = favouriteDao.queryByCondition(userId, entityType, entityKey);
            if (isFavourite) {
                if (favourite == null) {
                    favouriteDao.insertFavourite(userId, entityType, entityKey);
                } else {
                    throw new UnexpectedStatusException(BizStatus.FAVOURITED);
                }
            } else {
                if (favourite == null) {
                    throw new UnexpectedStatusException(BizStatus.UNFAVOURITED);
                } else {
                    favouriteDao.deleteByCondition(userId, entityType, entityKey);
                }
            }

            int favouriteCount = favouriteDao.queryByCondition(entityType, entityKey);
            PersistReqBuilder rb = PersistFactory.persistReqBuilder(CrmSchema.CRM, entityType);
            rb.addTargets(PersistTarget.ES);
            // SourceBuilder
            SourceBuilder sb = PersistFactory.sourceBuilder(entityKey);
            sb.updateNormalField("favorite_count", favouriteCount);
            rb.addSource(sb);

            persistService.bulkPersist(rb.get());

            return Response.success();
        } catch (UnexpectedStatusException e) {
            throw new UnexpectedStatusException(e.getStatus());
        }
    }

    @Override
    public Response status(String entityType, String entityKey) {
        try {
            Long userId = CommonUtils.getUserID();
            FavouriteDo favourite = favouriteDao.queryByCondition(userId, entityType, entityKey);
            Map<String, Boolean> result = new HashMap<>();
            if (null == favourite) {
                result.put("status", false);
            } else {
                result.put("status", true);
            }
            return Response.success(result);
        } catch (Exception e) {
            throw new UnexpectedStatusException(BizStatus.FIND_FAVOURITE_STATUS_ERROR, e);
        }
    }

}
